<script>
export default {
  name: "Authority",
  data() {
    return {
      Authorities: localStorage.getItem("authorities") ? JSON.parse(localStorage.getItem("authorities")) : {},
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      id: undefined,
      authorityCode: undefined,
      authorityName: "",
      authority: {},
      dialogFormVisible: false,
      ifSelecting: false,
      EditDisabled: false,
    }
  },
  created(){
    this.firstLoad();
    this.EditDisabled = this.Authorities.Authority19;
  },
  methods: {
    firstLoad() {
      this.request.get("http://localhost:9090/authority/all", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
    },
    load() {
      this.request.get("http://localhost:9090/authority/page",  {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          authorityName: this.authorityName,
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
      this.ifSelecting = true;
    },
    save() {
      this.request.post("http://localhost:9090/authority/save", this.authority).then(res => {
        if (res) {
          this.$message.success("保存成功");
          this.dialogFormVisible = false;
          this.firstLoad();
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    handleEdit(row) {
      this.authority = row;
      this.authority = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    reset(){
      this.pageNum = 1;
      this.authorityName = "";
      this.ifSelecting = false;
      this.firstLoad();
    },
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize = pageSize
      if (this.ifSelecting) {
        this.load()
      } else {
        this.firstLoad()
      }
    },
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum = pageNum
      if (this.ifSelecting) {
        this.load()
      } else {
        this.firstLoad()
      }
    }
  }
}
</script>

<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 150px" placeholder="请输入权限名" suffix-icon="el-icon-message" class="ml-5" v-model="authorityName" :disabled="!this.Authorities.Authority18"></el-input>
      <el-button class="ml-5" type="primary" @click="load" :disabled="!this.Authorities.Authority18">搜索</el-button>
      <el-button type="warning" @click="reset" :disabled="!this.Authorities.Authority18">重置</el-button>
    </div>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg">
      <el-table-column prop="id" label="ID" width="140"></el-table-column>
      <el-table-column prop="authorityCode" label="权限编码" width="200"></el-table-column>
      <el-table-column prop="authorityName" label="权限名" width="200"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" :disabled="!EditDisabled">编辑<i class="el-icon-edit"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="修改权限名" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="100px">
        <el-form-item label="权限名">
          <el-input v-model="authority.authorityName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>

</style>